<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>用户信息</span>
      </div>
      <el-form :data="user" label-width="70px" class="demo-ruleForm">
        <el-form-item label="用户名">
          <el-input v-model="user.username" disabled=""></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="user.nickname" placeholder="请输入昵称"></el-input>
        </el-form-item>
        <el-form-item label="原密码">
          <el-input v-model="user.oldPassword" placeholder="请输入原密码" show-password></el-input>
        </el-form-item>
        <el-form-item label="新密码">
          <el-input v-model="user.newPassword" placeholder="请输入新密码" show-password></el-input>
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input placeholder="请输入手机号码" v-model="user.phone"></el-input>
        </el-form-item>
        <el-button :plain="true" type="primary" @click="submitForm()">提交</el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      user: {username: "", nickname: "", oldPassword: "", newPassword: "", phone: ""},
    }
  },
  methods: {
    loadUser() {
      let url = 'http://localhost:9580/users/user';
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        this.user = responseBody.data;
      });
    },

    submitForm() {
      let url = 'http://localhost:9580/users/user/update';
      console.log('url = ' + url);

      let formData = this.qs.stringify(this.user);

      console.log('formData = ' + formData);


      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url,formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
              this.$router.push("/");
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadUser();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    }
  },
  mounted() {
    this.loadUser();
  }
}
</script>

<style>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

.box-card {
  width: 480px;
  /*height: px;*/
  margin: 200px auto;
  text-align: center;
}

.el-input {
  width: 360px;
  margin-bottom: 30px;
}

</style>